#include "organizationdao.h"
#include"../utils/database.h"
#include"QSqlQuery"
#include<QSqlRecord>
#include<QVariant>
#include<QSqlError>
#include<QDebug>

OrganizationDao::OrganizationDao()
{
    QSqlQuery result=QSqlQuery(*DataBase::getInstance());
}

/*
 * 新增组织
 */
void OrganizationDao::addOrg(OrganizationInfo& org)
{
    QString sql="insert into organizationInfo(id,name,address,creditCard) value(%1,%2,%3,%4)";
    sql=sql.arg(org.id)
            .arg(QString::fromStdString(org.name))
            .arg(QString::fromStdString(org.address))
            .arg(QString::fromStdString(org.creditCard));
//    if(result.exec(sql))
//        return true;
//    else
//    {
//        qDebug()<<result.lastError().text();
//    }
//    return false;
}

/*
 * 修改组织
 */
bool OrganizationDao::alterOrg(OrganizationInfo org)
{
//    int id;
//    string name;
//    string address;
//    string creditCard;

    QString sql="update organizationInfo set id=%1 name=%2 address=%3 creditCard=%4";
    sql=sql.arg(org.id)
            .arg(QString::fromStdString(org.name))
            .arg(QString::fromStdString(org.address))
            .arg(QString::fromStdString(org.creditCard));

     if(result.exec(sql))
     {
        return true;
       }
     else
     {
         qDebug()<<result.lastError().text();
     }
     return false;
}

/*
 * 删除组织
 */
bool OrganizationDao::delOrg(OrganizationInfo org)
{
    QString sql="delete from organizationInfo where id = %1";
    sql=sql.arg(org.id);
    if(result.exec(sql))
          return true;
    else
    {
        qDebug()<<result.lastError().text();
    }
    return false;
}

/*
 * 查询所有组织
 */
vector<OrganizationInfo> OrganizationDao::selectAll()
{
    QString sql=("select * from organizationInfo");
    QSqlQuery result=DataBase::getInstance()->exec(sql);

    int count=result.size();
//    if(count<=0)
//        return false;
    vector<OrganizationInfo> ans;

    while (result.next()) {
        OrganizationInfo _Organizat;
        _Organizat.address = result.value("address").toString().toStdString();
        _Organizat.id = result.value("id").toInt();
        _Organizat.creditCard = result.value("creditCard").toString().toStdString();
        _Organizat.name = result.value("name").toString().toStdString();

        ans.push_back(_Organizat);
    }
    return ans;
}

//根据user查询所属的组织
vector<OrganizationInfo> OrganizationDao::selectOrg(User& user)
{
    vector<OrganizationInfo> vec;
    QString sql=("select * from organizationInfo where id in (select organization_id from joinApproval where user_id = '%1')");
    sql = sql.arg(QString::fromStdString(user.phone));
    bool k=result.exec(sql);
    if(!k)
    {
        qDebug()<<result.lastError().text();
    }
    while(result.next()){
        OrganizationInfo org;
        org.id = result.value("id").toInt();
        org.name = result.value("name").toString().toStdString();
        org.address =result.value("address").toString().toStdString();
        org.creditCard = result.value("creditCard").toString().toStdString();
        vec.push_back(org);
    }
    return vec;
}
